/*
 * JBeagle - a Java toolkit for genetic algorithms.
 * 
 * Copyright (c) 2010 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package jbeagle.core;

import java.util.Collection;

public class RealIndividualFactory extends ListIndividualFactory<Double> {

	public static final double  DEFAULT_MIN_INIT = -0.5, 
                                DEFAULT_MAX_INIT = 0.5;
	
	protected double mininit, maxinit;
	
	public RealIndividualFactory( int minlen, int maxlen ) {
		this( minlen, maxlen, DEFAULT_MIN_INIT, DEFAULT_MAX_INIT );
	}
	
	public RealIndividualFactory( int minlen, int maxlen, double mininit, double maxinit ) {
		super( minlen, maxlen );
		this.mininit = mininit;
		this.maxinit = maxinit;
	}
    
    public void setIndividualBoundaries(final double minval, final double maxval) {
        setConstraints( new Constraints<Double>(){
            protected boolean violates(Double d) {
                if(d < minval || d > maxval)
                    return true;
                return false;
            }
        });
    }
	
    @Override
	protected void fillIndividual( ListIndividual<Double> ind, int len) {
		for ( int i = 0; i < len; i++ )
			ind.add( mininit + Math.random() * (maxinit - mininit) );
	}
    
    @Override
	protected void fillIndividual( ListIndividual<Double> ind, Collection<String> values) {
		for ( String val : values )
			ind.add( Double.parseDouble(val) );
	}

}